﻿using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Magic_Folder
{
    public partial class Program_Item : UserControl
    {
        public Program_Item()
        {
            InitializeComponent();
            this.LostFocus += Program_Item_LostFocus;
            this.GotFocus += Program_Item_GotFocus;
            txt_Path_TextChanged(this, null);
        }

        void Program_Item_GotFocus(object sender, EventArgs e)
        {
            this.BackColor = Color.SteelBlue;
        }

        void Program_Item_LostFocus(object sender, EventArgs e)
        {
            this.BackColor = Color.WhiteSmoke;
        }


        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btn_selectPath_Click(object sender, EventArgs e)
        {
            if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txt_Path.Text = folderBrowserDialog1.SelectedPath.ToString();
            }
        }

        private void txt_Path_TextChanged(object sender, EventArgs e)
        {
            if (Directory.Exists(System.Environment.ExpandEnvironmentVariables(txt_Path.Text.ToString())))
            {
                lbl_Path.Text = System.Environment.ExpandEnvironmentVariables(txt_Path.Text.ToString()).ToString();
                lst_Executables.Items.Clear();
                var files = Directory.EnumerateFiles(lbl_Path.Text, "*.*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".exe") || s.EndsWith(".dll"));
                foreach (string currentFile in files)
                {
                    lst_Executables.Items.Add(Path.GetFileName(currentFile));
                }
                if (lst_Executables.Items.Count > 0)
                {
                    lst_Executables.Enabled = true;

                }
                else
                {
                    pictureBox1.Image = Magic_Folder.Properties.Resources.help_icon;
                    lbl_executablePath.Text = "";
                    lst_Executables.Enabled = false;
                }  
            }
            else
            {
                lbl_Path.Text = "Not Found";
            }
        }

        private void lst_Executables_SelectedIndexChanged(object sender, EventArgs e)
        {
            string filepath = lbl_Path.Text + "\\" + lst_Executables.SelectedItem.ToString();
            if (File.Exists(filepath))
            {
                    lbl_executablePath.Text = filepath;
                    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(filepath);
                    if (myFileVersionInfo.InternalName != null)
                    {
                        lbl_FileDescription.Text = myFileVersionInfo.InternalName.ToString();
                    }
                    else
                    {
                        lbl_FileDescription.Text = lst_Executables.SelectedItem.ToString();
                    }

                    Icon ico = Icon.ExtractAssociatedIcon(filepath);
                    pictureBox1.Image = ico.ToBitmap();
                }
        }
        

        private void Program_Item_Load(object sender, EventArgs e)
        {
        }

        private void Program_Item_MouseEnter(object sender, EventArgs e)
        {

            
        }

        private void Program_Item_Leave(object sender, EventArgs e)
        {
            if (this.ContainsFocus)
            {
                this.BackColor = Color.SteelBlue;
            }
            else
            {
                this.BackColor = Color.WhiteSmoke;
            }
        }

        private void Program_Item_Enter(object sender, EventArgs e)
        {
            if (this.ContainsFocus)
            {
                this.BackColor = Color.SteelBlue;
            }
            else
            {
                this.BackColor = Color.WhiteSmoke;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Parent.Controls.Remove(this);
        }

        private void pictureBox2_MouseEnter(object sender, EventArgs e)
        {
            pictureBox2.Image = Magic_Folder.Properties.Resources.trash_selected;
        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            pictureBox2.Image = Magic_Folder.Properties.Resources.trash;

        }

        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            this.Parent.Controls.Remove(this);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            comboBox2.Enabled = checkBox1.Checked;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("If you want an default action called whever this program is run, this is where to do it. Check to enable.","Information",MessageBoxButtons.OK,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);
        }

    }
}
